#include <Windows.h>
#include <tchar.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static PROCESS_INFORMATION pi = {0};
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0074"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			

			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNEXIT:
				{
					PostQuitMessage(0);
					break;
				}
				case IDC_BTNSTARTUP:
				{
					STARTUPINFO si = {0};
					si.cb	= sizeof(si);
					CreateProcess(_T("C:\\WINDOWS\\NotePad.exe"), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
					WaitForInputIdle(pi.hProcess, 5000);
					SendDlgItemMessage(hDlg, IDC_STCINFO, WM_SETTEXT, 0, (LPARAM)_T("Startup successfully"));

					break;
				}
				case IDC_BTNCLEARUP:
				{
					DWORD dwExitCode = 0;
					GetExitCodeProcess(pi.hProcess, &dwExitCode);
					if (dwExitCode == STILL_ACTIVE)
					{
						TerminateProcess(pi.hProcess, dwExitCode);
					}
					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}